package com.whut.whw.util;

import android.support.design.widget.AppBarLayout;
import android.util.Log;

/**
 * <pre>
 *  desc:用于监听CollapsingToolbarLayout的展开和折叠
 *  Created by 忘尘无憾 on 2017/06/20.
 *  version:
 * </pre>
 */
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {

    /**
     * EXPANED展开
     * COLLAPSED折叠
     * IDLE中间态
     */
    public enum State {
        EXPANED,
        COLLAPSED,
        IDLE
    }

    private State mCurrentState = State.IDLE;

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//        Log.e("tc",verticalOffset+"---"+appBarLayout.getTotalScrollRange());
        if (verticalOffset == 0) {//展开
            if (mCurrentState != State.EXPANED) {
                onStateChanged(appBarLayout, State.EXPANED);
            }
            mCurrentState = State.EXPANED;
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {//折叠
            if (mCurrentState != State.COLLAPSED) {
                onStateChanged(appBarLayout, State.COLLAPSED);
            }
            mCurrentState = State.COLLAPSED;
        } else {//中间状态
            if (mCurrentState != State.IDLE) {
                onStateChanged(appBarLayout, State.IDLE);
            }
            mCurrentState = State.IDLE;
        }
    }

    /**
     * 抽象方法，用于检测三种状态
     *
     * @param appBarLayout
     * @param state
     */
    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
